﻿using System.Diagnostics;
using System.Drawing;
using System.IO;
using FluentAssertions;
using NUnit.Framework;

namespace Xnatlas.Tests
{
    [TestFixture]
    public class BuildAtlasTests
    {
        [Test]
        public void Axe()
        {
            BitmapAtlas atlas = new BitmapAtlas(64, 64, @"xx");
            AtlasedTexture bob = atlas.EnqueueTexture("bob", new BitmapTextureWrapper(new Bitmap(30, 62), "bob"));
            AtlasedTexture steve = atlas.EnqueueTexture("steve", new BitmapTextureWrapper(new Bitmap(1, 1), "steve"));
            atlas.AppendQueuedTextures();
            Debug.WriteLine("bob = " + bob);
            Debug.WriteLine("steve = " + steve);
            Assert.IsFalse(bob.AtlasAreaRectangle.Intersects(steve.AtlasAreaRectangle), "Bob and Steve intersect!");
        }

        [Test]
        public void BuildActualAtlas()
        {
            BitmapAtlas atlas = new BitmapAtlas(1024, 1024, @"..\..\Source Images\");

            string[] files = Directory.GetFiles(atlas.FilePath);

            foreach (string fileWithPath in files)
            {
                string file = Path.GetFileName(fileWithPath);
                file = Path.GetFileNameWithoutExtension(file);
                //Debug.WriteLine(file);
                if (file != "atlas")
                {
                    atlas.EnqueueTexture(file, "MyData" + file + "bob");
                }
            }

            atlas.AppendQueuedTextures();
            atlas.SaveToFile("atlas", StorageType.Json);

            BitmapAtlas otherBitmapAtlas = new BitmapAtlas(@"..\..\Source Images\", "atlas", StorageType.Json);

            foreach (AtlasedTexture original in atlas.Textures)
            {
                AtlasedTexture copy = otherBitmapAtlas.GetAtlasedTexture(original.FileName);
                original.Height.Should().Be(copy.Height);
                original.Width.Should().Be(copy.Width);
                original.OriginalTexture.Data.Should().Be(copy.OriginalTexture.Data);
                original.AtlasAreaRectangle.X.Should().Be(copy.AtlasAreaRectangle.X);
                original.AtlasAreaRectangle.Y.Should().Be(copy.AtlasAreaRectangle.Y);
                original.AtlasAreaRectangle.Width.Should().Be(copy.AtlasAreaRectangle.Width);
                original.AtlasAreaRectangle.Height.Should().Be(copy.AtlasAreaRectangle.Height);
                original.UvCoordinateBottomRight.X.Should().Be(copy.UvCoordinateBottomRight.X);
                original.UvCoordinateBottomRight.Y.Should().Be(copy.UvCoordinateBottomRight.Y);
                original.UvCoordinateTopLeft.X.Should().Be(copy.UvCoordinateTopLeft.X);
                original.UvCoordinateTopLeft.Y.Should().Be(copy.UvCoordinateTopLeft.Y);
            }
        }
    }
}
